import { _modify } from '../../common/api/usercenter'
import { _code } from '../../common/api/signin'
import { NAME_REG, TEL_REG, ID_CHECK } from '../../utils/reg'
const app = getApp()
const countMax = 60
const keyMap = {
  'AgentName': 'name',
  'Phone': 'tel',
  'CardID': 'idnum',
  'BankCard': 'bankcardnum',
  'BankName': 'bankname',
  'BranchBankName': 'subbankname'
}
Page({
  data: {
    memberType: '',
    name: '',
    tel: '',
    idnum: '',
    bankcardnum: '',
    bankname: '',
    subbankname: '',
    field: '',
    key: '',
    countNum: countMax,
    countMax,
    code: ''
  },
  inputHandler (e) {
    let attr = keyMap[this.data.field]
    this.data[attr] = e.detail.value
    this.setData({
      key: attr,
      [attr]: this.data[attr]
    })
  },
  codeInputHandler(e) {
    this.setData({
      code: e.detail.value
    })
  },
  getCode() {
    if (!TEL_REG.test(this.data.tel)) {
      app.toast('请输入正确格式的手机号码')
      return
    }
    app.loading('加载中')
    _code(this.data.tel).then(res => {
      wx.hideLoading()
      wx.showModal({
        title: res.data.IsSuccess ? '温馨提示' : '对不起',
        content: res.data.Msg,
        showCancel: false
      })
      console.log(res.data.Data)
      this.data.rcode = res.data.IsSuccess ? res.data.Data : ''
      this.countDown()
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求失败，请稍后再试',
        showCancel: false
      })
    })
  },
  countDown() {
    let timer = setInterval(() => {
      if (this.data.countNum < 1) {
        clearInterval(timer)
        this.data.countNum = countMax
        this.setData({
          countNum: this.data.countNum
        })
        return
      }
      this.data.countNum--
      this.setData({
        countNum: this.data.countNum
      })
    }, 1000)
  },
  confirm () {
    if (this.data.field === 'AgentName') {
      if (!NAME_REG.test(this.data.name)) {
        app.toast('请输入正确格式的中文姓名')
        return
      }
    }
    if (this.data.field === 'Phone') {
      if (!TEL_REG.test(this.data.tel)) {
        app.toast('请输入正确格式的手机号码')
        return
      }
      if (!this.data.code.trim()) {
        app.toast('请输入验证码')
        return
      }
    }
    if (this.data.field === 'CardID') {
      if (!ID_CHECK(this.data.idnum)) {
        app.toast('请输入有效的身份证号')
        return
      }
    }
    if (!this.data[this.data.key].trim()) {
      app.toast(`${this.data.title}不能为空`)
      return
    }
    let uid = wx.getStorageSync('uid')
    let val = this.data[keyMap[this.data.field]]
    app.loading('加载中')
    _modify(uid, this.data.field, val, this.data.code).then(res => {
      wx.hideLoading()
      wx.showModal({
        title: res.data.IsSuccess?'温馨提示':'对不起',
        content: res.data.Msg,
        showCancel: false,
        success: r => {
          if (r.confirm && res.data.IsSuccess) {
            wx.navigateBack()
          }
        }
      })
    }).catch(err => {
      console.log(err)
      wx.hideLoading()
      wx.showModal({
        title: '对不起',
        content: '请求失败，请稍后再试',
        showCancel: false
      })
    })
  },
  onLoad (options) {
    let attr = Object.keys(options)[0]
    this.data.key = attr
    let title = '资料'
    switch (attr) {
      case 'memberType':
        title = '会员类型'
        break
      case 'name':
        title = '姓名'
        this.data.field = 'AgentName'
        break
      case 'tel':
        title = '手机号'
        this.data.field = 'Phone'
        break
      case 'idnum':
        title = '身份证号'
        this.data.field = 'CardID'
        break
      case 'bankcardnum':
        title = '银行卡号'
        this.data.field = 'BankCard'
        break
      case 'bankname':
        title = '银行名称'
        this.data.field = 'BankName'
        break
      case 'subbankname':
        title = '分行名称'
        this.data.field = 'BranchBankName'
        break
      default:
        break
    }
    wx.setNavigationBarTitle({
      title: '修改' + title
    })
    this.setData({
      title: title,
      [attr]: options[attr],
      field: this.data.field
    })
  },
  onReady () {},
  onShow() { },
  onShareAppMessage() {
    return app.shareInfo
  }
})